/* 
 * Copyright (C) 2001-2003 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#if !defined(AFX_PRIVATEFRAME_H__8F6D05EC_ADCF_4987_8881_6DF3C0E355FA__INCLUDED_)
#define AFX_PRIVATEFRAME_H__8F6D05EC_ADCF_4987_8881_6DF3C0E355FA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "../client/User.h"
#include "../client/CriticalSection.h"
#include "../client/ClientManagerListener.h"
#include "../client/ResourceManager.h"

#include "FlatTabCtrl.h"
#include "WinUtil.h"
#include "UCHandler.h"

#define PM_MESSAGE_MAP 8		// This could be any number, really...
// pothead extension starts #13 svn50
#define CLEAR_MESSAGE_MAP 19
// pothead extension ends

class PrivateFrame : public MDITabChildWindowImpl<PrivateFrame, RGB(0, 255, 255)>, 
	private ClientManagerListener, public UCHandler<PrivateFrame>
{
public:
	static void gotMessage(const User::Ptr& aUser, const tstring& aMessage);
	static void openWindow(const User::Ptr& aUser, const tstring& aMessage = Util::emptyString);
	static bool isOpen(const User::Ptr u) { return frames.find(u) != frames.end(); };
	//Gabberworld starts svn 43/68 
	static void sound(string wavsound);
	static void onCloseAllPm();
	//Gabberworld ends

	enum {
		USER_UPDATED
	};

	DECLARE_FRAME_WND_CLASS_EX(_T("PrivateFrame"), IDR_PRIVATE, 0, COLOR_3DFACE);

	virtual void OnFinalMessage(HWND /*hWnd*/) {
		delete this;
	}

	typedef MDITabChildWindowImpl<PrivateFrame, RGB(0, 255, 255)> baseClass;
	typedef UCHandler<PrivateFrame> ucBase;

	BEGIN_MSG_MAP(PrivateFrame)
		MESSAGE_HANDLER(WM_SETFOCUS, onFocus)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_CTLCOLOREDIT, onCtlColor)
		MESSAGE_HANDLER(WM_CTLCOLORSTATIC, onCtlColor)
		MESSAGE_HANDLER(WM_CLOSE, onClose)
		MESSAGE_HANDLER(WM_SPEAKER, onSpeaker)
		MESSAGE_HANDLER(FTM_CONTEXTMENU, onTabContextMenu)
		COMMAND_ID_HANDLER(IDC_GETLIST, onGetList)
		COMMAND_ID_HANDLER(IDC_MATCH_QUEUE, onMatchQueue)
		COMMAND_ID_HANDLER(IDC_GRANTSLOT, onGrantSlot)
		COMMAND_ID_HANDLER(IDC_ADD_TO_FAVORITES, onAddToFavorites)
		COMMAND_ID_HANDLER(IDC_SEND_MESSAGE, onSendMessage)
		COMMAND_ID_HANDLER(IDC_CLOSE_WINDOW, onCloseWindow)
		// pothead extension starts (r2 code) svn61 svn120
		COMMAND_ID_HANDLER(ID_EDIT_COPY, onEditCopy)
		COMMAND_ID_HANDLER(IDC_OPEN_USER_LOG, onOpenUserLog)
		// pothead extension ends
		CHAIN_COMMANDS(ucBase)
		CHAIN_MSG_MAP(baseClass)
	ALT_MSG_MAP(PM_MESSAGE_MAP)
		// pothead starts svn110
		MESSAGE_HANDLER(WM_LBUTTONDBLCLK, onLButton)
		MESSAGE_HANDLER(WM_CONTEXTMENU, onContextMenu)
		// pothead ends
		MESSAGE_HANDLER(WM_CHAR, onChar)
		MESSAGE_HANDLER(WM_KEYDOWN, onChar)
		MESSAGE_HANDLER(WM_KEYUP, onChar)
	// pothead extension starts svn50
	ALT_MSG_MAP(CLEAR_MESSAGE_MAP)
		COMMAND_ID_HANDLER(IDC_CLEAR, onClear)
	// pothead extension ends
	//Sulan start svn64
		COMMAND_ID_HANDLER(IDC_LOG, onOpenUserLog)
		//End
	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onChar(UINT uMsg, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onGetList(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onMatchQueue(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onGrantSlot(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onAddToFavorites(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onTabContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	// pothead extension starts #13 svn50, (r2 code) svn61 svn110 svn120
	LRESULT onClear(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onOpenUserLog(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onLButton(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	// pothead extension ends
	//Sulan start svn109
	static DWORD CALLBACK funWithWin32CallBacks(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);
	string currentChatMessage;
	//End

	void addLine(const tstring& aLine);
	void onEnter();
	void onTab();
	void UpdateLayout(BOOL bResizeBars = TRUE);	
	void runUserCommand(UserCommand& uc);
	tstring readLog();
	
	LRESULT onClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	
	LRESULT onSendMessage(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		onEnter();
		return 0;
	}

	LRESULT onCloseWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT PrivateFrame::onSpeaker(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /* bHandled */) {
		updateTitle();
		return 0;
	}
	
	LRESULT onCtlColor(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
		HWND hWnd = (HWND)lParam;
		HDC hDC = (HDC)wParam;
		if(hWnd == ctrlClient.m_hWnd || hWnd == ctrlMessage.m_hWnd) {
			::SetBkColor(hDC, WinUtil::bgColor);
			::SetTextColor(hDC, WinUtil::textColor);
			return (LRESULT)WinUtil::bgBrush;
		}
		bHandled = FALSE;
		return FALSE;
	};

	LRESULT onFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
		ctrlMessage.SetFocus();
		return 0;
	}
	
	void addClientLine(const tstring& aLine) {
		if(!created) {
			CreateEx(WinUtil::mdiClient);
		}
		//Sulan start svn64
		ctrlStatus.SetText(1, (_T("[") + Util::getShortTimeString() + _T("] ") + aLine).c_str());
		//End
		setDirty();
	}
	
	void setUser(const User::Ptr& aUser) { user = aUser; };
	void sendMessage(const tstring& msg) {
		if(user && user->isOnline()) {
			string s = "<" + user->getClientNick() + "> " + msg;
			user->privateMessage(s);
			addLine(s);
		}
	}
	
	User::Ptr& getUser() { return user; };
	// CraKteR extension start
	string getUserNick() { return user->getNick(); };
	string getIP() { return user->getIp(); };
	// CraKteR extension end

	// pothead starts svn120
	LRESULT onEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		ctrlClient.Copy();
		return 0;
	}
	// pothead ends
private:
	PrivateFrame(const User::Ptr& aUser) : user(aUser), 
		created(false), closed(false), 
		// pothead extension starts #13 svn50 svn249
		clearContainer(_T("BUTTON"), this, CLEAR_MESSAGE_MAP),
		statusContainer(STATUSCLASSNAME, this, CLEAR_MESSAGE_MAP),
		notNewPm(false),
		// pothead extension ends
		//Sulan start svn64
		logContainer(_T("BUTTON"), this, CLEAR_MESSAGE_MAP),
		//Sulan start svn109
		clientContainer(_T("edit"), this, PM_MESSAGE_MAP),
		//End
		ctrlMessageContainer(_T("edit"), this, PM_MESSAGE_MAP) {
	}
	
	~PrivateFrame() {
	}
	
	bool created;
	typedef HASH_MAP<User::Ptr, PrivateFrame*, User::HashFunction> FrameMap;
	typedef FrameMap::iterator FrameIter;
	static FrameMap frames;
	//Sulan start svn109
	CRichEditCtrl ctrlClient;
	CContainedWindow clientContainer;
	//End
	CEdit ctrlMessage;
	CStatusBarCtrl ctrlStatus;
	static CriticalSection cs;
	
	CMenu tabMenu;

	StringMap ucParams;

	User::Ptr user;
	CContainedWindow ctrlMessageContainer;
	// pothead extension starts #13 svn50 svn61 svn120
	CButton ctrlClear;
	CContainedWindow clearContainer, statusContainer;
	LPCTSTR sMyNick;
	CMenu editMenu;
	// pothead extension ends
	//Sulan start svn64
	CButton ctrlLog;
	CContainedWindow logContainer;
	//End

	bool closed;
	bool notNewPm;
	//Gabberworld starts new_svn45 (r2 code)
	StringList prevCommands;
	tstring currentCommand;
	StringList::size_type curCommandPosition;
	//Gabberworld ends

	void updateTitle() {
		if(user->isOnline()) {
			SetWindowText(user->getFullNick().c_str());
			setTabColor(RGB(0, 255, 255));
			// pothead starts svn249
			if (BOOLSETTING(ONLINE_OFFLINE_PM) && notNewPm) {
				if (BOOLSETTING(TIME_STAMPS))
					addLine(_T("*** ") + TSTRING(USER_RETURNED));
				else
					addLine(_T("*** ") + TSTRING(USER_RETURNED) + _T(" at ") + Util::getShortTimeString());
			notNewPm = false; //gabberwold was here
			}
			// pothead ends
		} else {
			if(user->getClientName() == TSTRING(OFFLINE)) {
				SetWindowText(user->getFullNick().c_str());
			} else {
				SetWindowText((user->getFullNick() + _T(" [") + TSTRING(OFFLINE) + "]").c_str());
				// pothead starts svn233 svn249
				if (BOOLSETTING(ONLINE_OFFLINE_PM))
					if (BOOLSETTING(TIME_STAMPS))
						addLine(_T("*** ") + TSTRING(USER_WENT_OFFLINE));
					else
						addLine(_T("*** ") + TSTRING(USER_WENT_OFFLINE) + _T(" at ") + Util::getShortTimeString());
				// pothead ends
			}
			notNewPm = true;
			setTabColor(RGB(255, 0, 0));
		}
	}
	
	// ClientManagerListener
	virtual void on(ClientManagerListener::Types type, const User::Ptr& aUser) throw();

	StringMap CountryNick;
};

#endif // !defined(AFX_PRIVATEFRAME_H__8F6D05EC_ADCF_4987_8881_6DF3C0E355FA__INCLUDED_)

/**
 * @file
 * $Id: PrivateFrame.h Last changed: 2005-07-15 14:17:57 By: Pothead for SVN: 250 Exp$
 */
